package com.shunbo.convenient.controller;

import com.shunbo.convenient.resultVo.CouponOrderVo;
import com.shunbo.convenient.resultVo.CustomerInfoVo;
import com.shunbo.convenient.service.*;
import com.shunbo.convenient.utils.ConvenientResult;
import com.shunbo.convenient.vo.*;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * 推广
 * @author weizhuang
 */
@RestController
@RequestMapping("/extension")
public class ExtensionController {

    @Resource
    private CustomerInfoService customerInfoService;

    @Resource
    private StoreService storeService;

    @Resource
    private CouponService couponService;

    @Resource
    private CouponOrderService couponOrderService;


    /**
     * 推广列表
     *
     * @return Result结果集
     */
    @RequestMapping("/list")
    public ConvenientResult getExtensionList() {
        try {
            List<CouponOrderVo> list = new ArrayList<>();
            List<Store> all1 = storeService.findAll(null);
            for (Store store: all1
                 ) {
                CouponOrderVo couponOrderVo = new CouponOrderVo();
                couponOrderVo.setStore(store);
                CustomerInfo customerInfo = new CustomerInfo();
                customerInfo.setSid(store.getId()+"");
                List<CustomerInfoVo> customerInfoVos = new ArrayList<>();
                List<CustomerInfo> all = customerInfoService.findAll(customerInfo);
                for (CustomerInfo customer:all
                     ) {
                    CustomerInfoVo customerInfoVo = new CustomerInfoVo();
                    customerInfoVo.setCustomerInfo(customer);
                    CouponOrder couponOrder = new CouponOrder();
                    couponOrder.setUserId(customer.getUserId());
                    List<CouponOrder> all2 = couponOrderService.findAll(couponOrder);
                    Coupon byId = couponService.findById(all2.get(all2.size() - 1).getId()+"");
                    customerInfoVo.setLastMoney(byId.getPrice()*all2.get(all2.size() - 1).getNum()+"");
                    customerInfoVo.setLastMoneyTime(all2.get(all2.size() - 1).getCreateTime());
                    Double resultMoney = 0.0;
                    for (CouponOrder couponOrder1:all2
                         ) {
                        Coupon cou = couponService.findById(couponOrder1.getId()+"");
                        resultMoney+=couponOrder.getNum()*cou.getPrice();
                    }
                    customerInfoVo.setMoney(resultMoney);
                    customerInfoVo.setPrice(resultMoney/100*store.getRate());
                    customerInfoVos.add(customerInfoVo);
                }
                couponOrderVo.setCustomerInfoVos(customerInfoVos);
                list.add(couponOrderVo);
            }


            return ConvenientResult.build(200, "成功", null);
        } catch (Exception e) {
            e.printStackTrace();
            return ConvenientResult.build(500, "系统错误", e.getStackTrace());
        }
    }
    /**
     * 推广订单信息
     *
     * @return Result结果集
     */
    @RequestMapping("/findOne")
    public ConvenientResult getExtensionFindOne() {
        try {
            List<CouponOrder> all = couponOrderService.findAll(null);
            for (CouponOrder couponOrder:all
                 ) {
                Coupon byId = couponService.findById(couponOrder.getCid()+"");
                CustomerInfo byId1 = customerInfoService.findById(couponOrder.getUserId());
                couponOrder.setCoupon(byId);
                couponOrder.setCustomerInfo(byId1);
            }
            return ConvenientResult.build(200, "成功", all);
        } catch (Exception e) {
            e.printStackTrace();
            return ConvenientResult.build(500, "系统错误", e.getStackTrace());
        }
    }

}
